
layui.use(['form','layarea','layer'], function () {
    let form = layui.form,
        layer = layui.layer,
        $ = layui.$,
        layarea = layui.layarea;

    /*城市级联*/
    layarea.render({
        elem: '#area-picker',

    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        //发异步，把数据提交给php
        $.ajax({
            url: '/admin/supplier/Save?action=ajax',
            type: 'post',
            dataType: 'json',
            data:data.field,
            success: function(res) {
                if (res.status == true) {
                    layer.msg('保存成功', {icon: 1, time: 1000},
                        function(){
                            parent.location.reload();
                        })

                }  else {
                    layer.open({
                        title: '错误信息',
                        content: '<span style="color:red">'+res.code+':</span>'+res.msg
                    })
                    return false
                }
            },
            error: function(re) {

                layer.open({
                    title: '错误信息',
                    content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                })
            }
        });

    });



    //添加分类
    $("#add-standard-price").on('click', function(){
        let catestr=$("#bizpro_class").html();
        let catearr=catestr.split(',');
        catearr.pop()
        let cateselect='<select name="bizpro_class[]" lay-filter="selectclass"><option value="供应商分类"></option>'
        let cateid=''
        let catetitle=''

        for(let i=0;i in catearr ;i++)
        {
            cateid=catearr[i].split('-')[0]
            catetitle=catearr[i].split('-')[1]

            cateselect+= '<option value="'+cateid+'">'+catetitle+'</option>'
        }
        cateselect+='</select>';

        let tr = $("#standard-price tr:last");
        let aa='<tr>\n' +
            '            <td>'+cateselect+'</td>\n' +
            '            <td><select name="bizpro_seclass[]" class="bizpro_seclass">\n' +
            '                    <option value="">请选择二级分类</option>\n' +
            '                </select></td>\n' +
            '            <td><button type="button"   class="layui-btn delete">删除</button></td>\n' +
            '        </tr>'
        if(tr.length==0){
            layer.alert("指定的table或对应的行数不存在！");
            return;
        }
        tr.after(aa);
        form.render();
        return false
    });

    //监听一级分类联动
    form.on('select(selectclass)',function (data) {
        let that=$(this)
        let id=data.value
        let seclass_re=''
        let seclass_str=''

        let p =new Promise(function (resolve, reject) {
            $.ajax({
                url: "/admin/bizpro/bizclass?pid="+data.value,
                type: 'post',
                async: true,
                dataType: 'json',
                success:function (res) {
                    resolve(res)
                },
                error:function (re) {
                    reject(re)
                }
            })
        })

        p.then(
            function (res)
            {
                seclass_re=res.data.data

               // return false
                // for(let i=0;i in seclass_re;i++)
                // {
                //     seclass_str+= '<input type="checkbox" name="bizpro_seclass[]" value="'+seclass_re[i].id+'" lay-skin="primary" title="'+seclass_re[i].title+'">'
                // }
                // that.eq(0).parents('tr').find('td').eq(1).html('')
                // that.eq(0).parents('tr').find('td').eq(1).html(seclass_str)
                let option_str='<option value="">请选择二级分类</option>';
                let data=seclass_re
                // console.log(that.eq(0).parents('tr').find('td').eq(1).find('.bizpro_seclass').html())
                // return false
                for (var i=0; i<data.length; i++) {
                    option_str+='<option value="'+data[i].id+'">'+data[i].title+'</option>';
                }
                that.eq(0).parents('tr').find('td').eq(1).find('.bizpro_seclass').html(option_str)
                form.render()

            },
            function (re) {
                layer.open({
                    title: '错误信息',
                    content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                })
            }
        )

    })

    //删除分类
    $("#standard-price").on("click",".delete",function(re){
        let obj=$(re.currentTarget).parents('tr')
        obj.remove()

    });
});







